<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               :upload-error="loadError"
               @on-load="onLoad" v-if="showMain">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.tjkjkc_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot-scope="{type,size,row,index}"  slot="menuBtn">
        <el-dropdown-item divided @click.native="showJobView(row,index)">课程任务</el-dropdown-item>
        <el-dropdown-item divided @click.native="showXsView(row,index)">报名学生</el-dropdown-item>
      </template>
    </avue-crud>
    <Tjkjkcjob v-bind:kcId="kcId"  :showMainView="showMainView" v-if="(!showMain)&&showJob" ref="eps1"></Tjkjkcjob>
    <Tjkjkcxs v-bind:kcId="kcId"  :showMainView="showMainView" v-if="(!showMain)&&showXs" ref="eps2"></Tjkjkcxs>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/service/tjkjkc";
  import {mapGetters} from "vuex";
  import Tjkjkcjob from "@/views/service/tjkjkcjob";

  import Tjkjkcxs from "@/views/service/tjkjkcxs";
  export default {
    components:{
      Tjkjkcjob,
      Tjkjkcxs
    }
    ,
    data() {
      return {
        kcId:"",
        showMain:true,
        showJob:false,
        showXs:false,
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          menuType:'menu',
          menuBtnTitle:'更多操作',
          column: [
            {
              label: "主键",
              prop: "id",
              rules: [{
                required: true,
                message: "请输入主键",
                trigger: "blur"
              }],
              hide:true,
              display:false
            },
            {
              label: "学校名称",
              prop: "schoolId",
              type: "tree",
              // search:true,
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入学校名称",
                trigger: "blur"
              }],
              dicUrl: "/api/PcPublicService/GetSchoolTreeSelect",
              props: {
                label: "xxName",
                value: "id"
              },
              span: 12,
              editDisabled:true
            },
            {
              label: "课程名称",
              prop: "kcName",
              overHidden:true,
              span: 12,
              rules: [{
                required: true,
                message: "请输入课程名称",
                trigger: "blur"
              }],
              maxlength:100,
              showWordLimit:true,
              editDisabled:true
            },
            {
              label: "课程介绍",
              prop: "kcDesc",
              type: "textarea",
              span: 24,
              minRows: 6,
              rules: [{
                required: true,
                message: "请输入课程介绍",
                trigger: "blur"
              }],
              hide: true,
              maxlength:500,
              showWordLimit:true,
            },
            {
              label: "课程标签",
              prop: "kcTags",
              span: 24,
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入课程标签",
                trigger: "blur"
              }],
              type: "select",
              dicData: [

                {
                  label: "自然活动",
                  value: "自然活动"
                },
                {
                  label: "生动有趣",
                  value: "生动有趣"
                }
              ],
              multiple:true,
              button:true,
              dataType:'string',
              hide: true
            },
            {
              label: "课程科目",
              prop: "kcKm",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入课程科目",
                trigger: "blur"
              }]  ,
              maxlength:50,
              showWordLimit:true,
            },
            {
              label: "课程地点",
              prop: "kcAddr",
              span: 12,
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入课程地点",
                trigger: "blur"
              }],
              maxlength:100,
              showWordLimit:true,
            },
            {
              label: "课程要求",
              prop: "kcRequires",
              type: "textarea",
              span: 24,
              minRows: 6,
              rules: [{
                required: true,
                message: "请输入课程要求",
                trigger: "blur"
              }],
              hide: true,
              maxlength:500,
              showWordLimit:true,
            },
            {
              label: "开始时间",
              prop: "kcStart",
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入开始时间",
                trigger: "blur"
              }],
              editDisabled:true,
            },
            {
              label: "结束时间",
              prop: "kcEnd",
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入结束时间",
                trigger: "blur"
              }],
              editDisabled:true,
            },
            {
              label: "学年",
              prop: "kcXnId",
              type: "select",
              overHidden:true,
              // search:true,
              rules: [{
                required: true,
                message: "请输入年级名称",
                trigger: "blur"
              }],
              dicUrl: "/api/school-service/xninit/select",
              props: {
                label: "xnName",
                value: "id"
              },
              editDisabled:true,

            },
            {
              label: "年级",
              prop: "kcNjId",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入年级ID",
                trigger: "blur"
              }],
              type:"select",
              dicUrl: "/api/school-service/nj/select",
              props: {
                label: "njName",
                value: "id"
              },
              editDisabled:true,
            },

            {
              label: "教师",
              prop: "kcJsId",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入教师ID",
                trigger: "blur"
              }],
              type: "select",
              dicUrl: "/api/school-service/js/select",
              props: {
                label: "jsName",
                value: "id"
              },
              dataType: "number",
              editDisabled:true
            },
            {
              label: "教师描述",
              prop: "kcJsDesc",
              type: "textarea",
              span: 24,
              minRows: 6,
              rules: [{
                required: true,
                message: "请输入教师描述",
                trigger: "blur"
              }],
              hide: true,
              maxlength:200,
              showWordLimit:true,
            },
            {
              label: "联系方式",
              prop: "kcJsPhone",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入教师联系方式",
                trigger: "blur"
              }],
              maxlength:20,
              showWordLimit:true,
            },
            {
              label: "课程类型",
              prop: "kcType",
              rules: [{
                required: true,
                message: "请输入课程类型",
                trigger: "blur"
              }],
              type: "select",
              dicData: [

                {
                  label: "探究课",
                  value: 1
                },
                {
                  label: "拓展课",
                  value: 2
                },
              ],
              value:1,
              dataType:'number',
              editDisabled:true,
            },
            {
              label: "课程人数",
              prop: "kcNumber",
              rules: [{
                required: true,
                message: "请输入课程人数",
                trigger: "blur"
              }],
              value:10,
              data:'number',
              type:'number',
            },
            {
              label: "已报名人数",
              prop: "kcAddNumber",
              rules: [{
                required: false,
                message: "请输入已报名人数",
                trigger: "blur"
              }],
              value:0,
              data:'number',
              type:'number',
              addDisabled:true,
              editDisabled:true
            },
            {
              label: "课程图片1",
              prop: "kcPic1",
              rules: [{
                required: true,
                message: "请输入课程图片1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              fileSize:520,
              action: '/api/kaibu-platform-service/oss/pri',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true
            },
            {
              label: "课程图片2",
              prop: "kcPic2",
              rules: [{
                required: false,
                message: "请输入课程图片2",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              fileSize:520,
              action: '/api/kaibu-platform-service/oss/pri',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true
            },
            {
              label: "课程图片3",
              prop: "kcPic3",
              rules: [{
                required: false,
                message: "请输入课程图片3",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              fileSize:520,
              action: '/api/kaibu-platform-service/oss/pri',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true
            },
            {
              label: "课程状态",
              prop: "kcStatus",
              rules: [{
                required: true,
                message: "请输入课程状态",
                trigger: "blur"
              }] ,
              type: 'select',
              dicUrl: "/api/blade-system/dict/dictionary?code=WENZHANGZHUANGTAI",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission",'JSuserInfo']),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.tjkjkc_add, false),
          viewBtn: this.vaildData(this.permission.tjkjkc_view, false),
          delBtn: this.vaildData(this.permission.tjkjkc_delete, false),
          editBtn: this.vaildData(this.permission.tjkjkc_edit, false)
        };
      },
      schoolId(){
        return this.JSuserInfo.schoolId
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      loadError(error){
        this.$message({
            type: "error",
            message: error
          });
      },
      showJobView(row){
        this.kcId = row.id;
        //this.$refs.eps.onLoad({currentPage:1,pageSize:10});
        this.showMain = false;
        this.showJob = true;
        this.showXs = false;
      },
      showXsView(row){
        this.kcId = row.id;
        //this.$refs.eps.onLoad({currentPage:1,pageSize:10});
        this.showMain = false;
        this.showXs = true;
        this.showJob = false;
      },
      showMainView(){
        this.showMain = true;
        this.showXs = false;
        this.showJob = false;
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        params.SchoolId = this.schoolId
        params.tenant_id = ''
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.totalItems;
          this.data = data.items;
          this.loading = false;
          this.selectionClear();
        });
        const school = this.findObject(this.option.column, "schoolId");
        school.hide = true;
      }
    }
  };
</script>

<style>
</style>
